﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Castle.ActiveRecord;
using Castle.ActiveRecord.Queries;

namespace Matriculex.Dominio
{
    [ActiveRecord("troca_disciplina")]
    public class TrocaDisciplina : RemovableEntityWithId<TrocaDisciplina>
    {
        // construtor vazio necessário para o ActiveRecord
        protected TrocaDisciplina() { }

        public TrocaDisciplina(AlunoAberto aluno, DateTime data, Disciplina disciplinaOriginal,
            Disciplina disciplinaNova)
        {
            this.Aluno = aluno;
            this.DisciplinaNova = disciplinaNova;
            this.DisciplinaOriginal = disciplinaOriginal;
            this.Data = data;
        }

        [BelongsTo]
        public Disciplina DisciplinaOriginal { get; set; }
        [BelongsTo]
        public Disciplina DisciplinaNova { get; set; }
        [BelongsTo]
        public AlunoAberto Aluno { get; set; }
        [Property]
        public DateTime? Data { get; set; }
        
        public static TrocaDisciplina[] FindAllByPeriodo(PeriodoRematricula periodo)
        {
            if (periodo == null)
            {
                return new TrocaDisciplina[0];
            }

            var query = new SimpleQuery<TrocaDisciplina>(
                @"from TrocaDisciplina t where :inicio <= t.Data and t.Data <= :fim");
            query.SetParameter("inicio", periodo.Inicio);
            query.SetParameter("fim", periodo.Fim);
            return query.Execute().Where(t => t.Ativado).ToArray();
        }
    }
}
